< previous page page_639 next page >

Page 639
image to the surface. Write a function that maps points on the surface back to points in the image. The point in the image will probably not fall exactly on one of the image's pixels, so use bilinear interpolation to find the best color to use.
These are the same steps described in Chapter 7 for stretching, shrinking, and warping images. The only tricky part is creating the function that maps points on the surface to points on the image.
Rectangles are among the easiest shapes to map. The program finds the coordinates of the point in the rectangle's frame of reference. It can then map those coordinates onto a rectangular image.
The RayCheckerboard class described earlier already maps a point into its own coordinate system to determine whether the point lies on one of its rectangular areas. This object is defined by a point at the corner of its first square and two vectors that define the edges of that square. To map a point, the object converts the coordinates of the point into multiples of these two vectors. For example, the point with checkerboard coordinates (1, 1) is at the opposite corner in the first square.
The RayMappedCheckerboard uses this same technique. To find a point's hit color, it uses these multiples to map the point to the object's image. For instance, if the point has checkerboard coordinates (0.5, 0.75), the FindHitColor function maps it to a position halfway through the image from right to left and three-quarters of the way through the image from top to bottom.
Once it has found a color for the point on the surface, the program can use that value to determine the surface's ambient and diffuse reflection parameters for that point. For example, suppose the color has red component 127. Since that is half of the maximum possible value 255, the program can set the object's red ambient and diffuse reflection coefficients to 0.5 at that point.
The RayMappedCheckerboard class uses this method. It also has its own ambient and diffuse reflection coefficients that it combines with the base values provided by the image.
Figure 17.32 shows three RayMappedCheckerboard objects each displaying a single square each. The objects' ambient and diffuse coefficients are all 1.0 and the light sources are white, so the results match the colors in the original images closely.
Special Environment Mapping
The basic environment mapping technique described in the previous section uses an image to determine the base color value for points on a surface. Similarly, you can use the pixels in an image to determine other characteristics of the point.
For example, the program could calculate the point's base color as before using an image. Then if the base color is black, the program can make the object transparent at that point. This lets objects behind the point show through.
The RayHoledCheckerboard class demonstrates this approach. It maps the points on its surface using an image just as the RayMappedCheckerboard class does. When it finds a

 
< previous page page_639 next page >